﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using VisaCRM.Modules.Admin.ViewModels.Helper;
using VisaCRM.Modules.Admin.Model;
using VisaCRM.Modules.Admin.Gateway;
using System.Windows.Input;

namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of countries
    /// </summary>
    class CountryViewModel:ViewModelBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
        public ObservableCollection<Country> allCountries;

        /// <summary>
        /// initialise gateway used
        /// </summary>
        CountryGateway countryGateway = new CountryGateway();

        ///summary
       ///initialises a new instance of CountryViewModel class
       ///summary
        public CountryViewModel(ObservableCollection<Country> allCountries)
       {
           currentCountry = new Country();
           if (allCountries == null || allCountries.Count == 0)
           {
               
               AllCountries = countryGateway.GetAllcountries();
           }
           else
           {
               this.AllCountries = allCountries;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllCountries.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentCountry))
               {
                   this.CurrentCountry = null;
               }
           };
           
 
           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.CloseCommand = new DelegateCommand((o) => this.Close());
       }

       public ICommand AddNewCommand { get; private set; }
       public ICommand SaveCommand { get; private set; }
       public ICommand CancelCommand { get; private set; }
       public ICommand CloseCommand { get; private set; }

       private void Save()
       {
           if (this.CurrentCountryName != null && this.CurrentCountryNationality!=null)
           {
               Country country = new Country() { CountryName = this.CurrentCountryName, CountryNationality = this.CurrentCountryNationality };
               bool flag = false;
               if (string.IsNullOrEmpty(country.CountryName))
                   return;
               foreach (Country con in this.AllCountries)
               {
                   if (con.CountryName == country.CountryName)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The country details are already listed");
                   return;
               }
               countryGateway.SaveNewCountryToDatabase(this.CurrentCountryName, this.CurrentCountryNationality, this.currentCountry.CountryEU, this.currentCountry.CountryMigration);
               AddNew();
               this.AllCountries = countryGateway.GetAllcountries();
               System.Windows.Forms.MessageBox.Show("The country details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill country and nationality details");
           }
       }

       private void AddNew()
       {
           this.CurrentCountryName = null;
           this.CurrentCountryNationality = null;
       }
       private void Cancel()
       {
           this.currentCountry = null;
       }
       private void Close()
       {
         
       }

       /// <summary>
       /// The deprtment currently selected in the workspace
       /// </summary>
       private Country  currentCountry;

       /// <summary>
       /// Gets or sets the deprtment currently selected in the workspace
       /// </summary>
       public Country CurrentCountry
       {
           get
           {
               return this.currentCountry;
           }

           set
           {
               this.currentCountry = value;
               if (this.CurrentCountry != null)
               {
                   this.CurrentCountryName = this.CurrentCountry.CountryName;
                   this.CurrentCountryNationality = this.CurrentCountry.CountryNationality;
               }
               this.OnPropertyChanged("CurrentCountry");
           }
       }

       /// <summary>
       /// The CountryName currently selected in the workspace
       /// </summary>
       private string currentCountryName;

       /// <summary>
       /// Gets or sets the CountryName currently selected in the workspace
       /// </summary>
       public string CurrentCountryName
       {
           get
           {
               return this.currentCountryName;
           }

           set
           {
               this.currentCountryName = value;
               this.OnPropertyChanged("CurrentCountryName");
           }
       }

       /// <summary>
       /// The CountryNationality currently selected in the workspace
       /// </summary>
       private string currentCountryNationality;

       /// <summary>
       /// Gets or sets the CountryNationality currently selected in the workspace
       /// </summary>
       public string CurrentCountryNationality
       {
           get
           {
               return this.currentCountryNationality;
           }

           set
           {
               this.currentCountryNationality = value;
               this.OnPropertyChanged("CurrentCountryNationality");
           }
       }

       /// <summary>
       /// Gets or sets the Country collection
       /// </summary>
       public ObservableCollection<Country> AllCountries
       {
           get
           {
               return this.allCountries;
           }

           set
           {
               this.allCountries = value;
               this.OnPropertyChanged("AllCountries");
           }
       }
    }
}
